import { maccApi, requestHttp } from '@utils/index';
const initState = {
  isNbr: false,
  gwSn: null,
  flowIpPolicyList: [],
  globalList: [],
  globalEnable: false,
  tplName: null,
  flowTemp: [],
  rule_intf: null, //nbr 才有
  ablityObj: {
    enable: false,
  },
  deviceVlan: [],
  applyGroup: [],
};

let state = {
  ...initState,
};

const mutations = {
  SET_GW_TYPE(state, payload) {
    if (payload == 'GATEWAY') {
      state.isNbr = true;
    } else {
      state.isNbr = false;
    }
  },
  SET_GW_SN(state, payload) {
    state.gwSn = payload;
  },

  SET_FLOW_DATA(state, payload) {
    const {
      flowIpPolicyList = [],
      globalList = [],
      globalEnable,
      tplName,
    } = payload;
    state.flowIpPolicyList = flowIpPolicyList;
    state.globalList = globalList;
    state.globalEnable = globalEnable;
    state.tplName = tplName;
  },

  SET_DEVICE_VLAN(state, payload) {
    state.deviceVlan = payload;
  },

  SET_ABILITY(state, payload) {
    state.ablityObj = payload;
  },

  SET_FLOW_TEMP(state, payload) {
    state.flowTemp = payload;
  },

  SET_RULE_INFT(state, payload) {
    state.rule_intf = payload;
  },

  SET_CUSTOME_APPLY_GROUP(state, payload) {
    state.applyGroup = payload;
  },

  CLEAR_STATE(state) {
    // state = { ...initState }; //不生效??
    Object.keys(initState).forEach((item) => {
      state[item] = initState[item];
    });
  },
};
const actions = {
  getGroupGwType({ commit }, params) {
    return new Promise((resolve, reject) => {
      let data = {
        api: `${maccApi.GET_GROUP_GATEWAY_TYP}?group_id=${params.groupId}`,
        method: 'GET',
      };
      requestHttp(data)
        .then((res) => {
          if (res && res.code == 0) {
            if (!res.data) {
              commit('SET_GW_TYPE', null);
              commit('SET_GW_SN', null);
              resolve(false);
              return;
            }
            const { sn, productType } = res.data;
            commit('SET_GW_TYPE', productType);
            commit('SET_GW_SN', sn);
            resolve(true);
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  getFlowControlAbility({ commit }) {
    return new Promise((resolve, reject) => {
      if (!state.gwSn) {
        commit('SET_ABILITY', {
          enable: false,
        });

        resolve();
        return;
      }
      requestHttp({
        api: `${maccApi.GET_FLOW_ABILITY.replace(
          '{sn}',
          state.gwSn
        )}?businessId=FLOW_CONFIG`,
        method: 'GET',
      })
        .then((res) => {
          const { code, data } = res;
          if (code == 0) {
            let obj;
            if (!data || data.length == 0) {
              obj = {
                enable: false,
              };
            } else {
              let abilityArr = [];
              data.forEach((item) => {
                const { abilityTypeList } = item;
                abilityArr = [...abilityArr, ...abilityTypeList];
              });
              obj = {
                enable: true,
                abilityTypeList: abilityArr,
              };
            }
            commit('SET_ABILITY', obj);
            resolve();
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  getFlowControlData({ commit }, params) {
    const querys = {
      all_intf: true,
      group_id: params.groupId,
    };
    if (state.isNbr && state.rule_intf) {
      querys.rule_intf = state.rule_intf;
    }
    return new Promise((resolve, reject) => {
      requestHttp({
        api: maccApi.GET_FLOWCROL_CONFIG,
        method: 'GET',
        querys,
      })
        .then((res) => {
          if (res && res.code == 0) {
            commit('SET_FLOW_DATA', res);
            const { flowIpPolicyList, globalList, globalEnable } = res;
            resolve({
              flowIpPolicyList,
              globalList,
              globalEnable,
            });
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  //获取应用组列表
  getCustomeApplyGroup({ commit }, params) {
    return new Promise((resolve, reject) => {
      requestHttp({
        api: maccApi.GET_CUSTOME_APPLU_GROUP,
        method: 'GET',
        querys: {
          sn: params.sn,
        },
      })
        .then((res) => {
          if (res && res.code == 0) {
            commit('SET_CUSTOME_APPLY_GROUP', res.data || []);
            resolve();
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  getFLowTemp({ commit }, params) {
    return new Promise((resolve, reject) => {
      requestHttp({
        api: `${maccApi.GET_FLOW_TEMP}?group_id=${params.groupId}`,
        method: 'GET',
      })
        .then((res) => {
          if (res && res.code == 0) {
            commit('SET_FLOW_TEMP', res.list || []);
            resolve();
          } else {
            reject(res);
          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

const getters = {
  portNames: (state) => {
    const arr = [];
    state.globalList.forEach((item) => {
      if (item.portName.startsWith('Di')) {
        arr.push(item.portName.toLowerCase());
      } else {
        arr.push(item.portName);
      }
    });
    return arr;
  },
  pannelNames: (state) => {
    const arr = [];
    state.globalList.forEach((item) => {
      arr.push(item.pannelName.toLowerCase());
    });
    return arr;
  },

  enableGlobalList: (state) => {
    const list = state.globalList.filter((item) => {
      return item.enable;
    });
    //排序
    const sortList = list.sort((a, b) => {
      const { portName: portName1 } = a;
      const { portName: portName2 } = b;
      if (portName1.indexOf('Di') != -1) {
        return 1;
      } else if (portName2.indexOf('Di') != -1) {
        return -1;
      } else if (portName1 < portName2) {
        return -1;
      } else {
        return 1;
      }
    });
    console.log('list22222', sortList);
    return sortList;
  },

  vlanIdArr: (state) => {
    return state.deviceVlan.map((item) => {
      return item.vlanId;
    });
  },
};

export default {
  namespaced: true, //开启命名空间，更好的区分使用stage,防止不同stage模块中的方法重名
  state,
  getters,
  mutations,
  actions,
};
